<template>
	<view class="add-pet-container">
		<view class="header">
			<image class="paw-print header-paw" src="/static/img/paw-print.svg" mode="aspectFit"></image>
			<text class="title">添加萌宠</text>
		</view>
		
		<view class="form-container">
			<view class="avatar-section">
				<view class="avatar-wrapper" @tap="chooseAvatar">
					<u-avatar size="120" :src="formData.avatar || '/static/img/default-pet-avatar.svg'"></u-avatar>
					<view class="upload-icon">
						<u-icon name="camera-fill" color="#fff" size="40"></u-icon>
					</view>
				</view>
				<text class="upload-text">点击上传头像</text>
			</view>
			
			<view class="form-group">
				<view class="form-item">
					<text class="label">萌宠名称</text>
					<input class="input" v-model="formData.name" placeholder="给萌宠起个名字吧" />
				</view>
				
				<view class="form-item">
					<text class="label">萌宠类型</text>
					<view class="type-selector">
						<view class="type-option" 
							:class="{'selected': formData.type === 'cat'}"
							@tap="formData.type = 'cat'">
							<image class="type-icon" src="/static/img/cat-icon.svg" mode="aspectFit"></image>
							<text class="type-text">猫咪</text>
						</view>
						<view class="type-option"
							:class="{'selected': formData.type === 'dog'}"
							@tap="formData.type = 'dog'">
							<image class="type-icon" src="/static/img/dog-icon.svg" mode="aspectFit"></image>
							<text class="type-text">狗狗</text>
						</view>
					</view>
				</view>
			</view>
			
			<view class="submit-section">
				<button class="submit-btn" @tap="submitForm">确认添加</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				formData: {
					avatar: '',
					name: '',
					type: 'cat',
					mood: 100,
					hunger: 100,
					energy: 100,
					exp: 0,
					level: 1,
					lastFeed: '',
					lastPlay: '',
					statistics: {
						totalFeeds: 0,
						totalPlays: 0,
						favoriteFood: '',
						moodHistory: []
					}
				},
				petTypes: ['猫咪', '狗狗'],
				typeIndex: 0,
				genderTypes: ['公', '母'],
				genderIndex: 0,
				showAdvancedSettings: false
			}
		},
		methods: {
			// 选择图片
			chooseAvatar() {
				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'],
					sourceType: ['album', 'camera'],
					success: async (res) => {
						const tempFilePath = res.tempFilePaths[0];
						
						try {
							uni.showLoading({ title: '上传中...' });
							
							// 上传图片到LeanCloud
							const file = new this.$leanCloud.File('pet-avatar.jpg', {
								blob: {
									uri: tempFilePath
								}
							});
							await file.save();
							
							this.formData.avatar = file.url();
							uni.hideLoading();
						} catch (error) {
							console.error('上传图片失败:', error);
							uni.showToast({
								title: '上传图片失败',
								icon: 'none'
							});
						}
					}
				});
			},
			
			// 类型选择
			onTypeChange(e) {
				this.typeIndex = e.detail.value;
				this.formData.type = this.typeIndex === 0 ? 'cat' : 'dog';
			},
			
			// 性别选择
			onGenderChange(e) {
				this.genderIndex = e.detail.value;
				this.formData.gender = this.genderIndex === 0 ? 'male' : 'female';
			},
			
			// 生日选择
			onBirthdayChange(e) {
				this.formData.birthday = e.detail.value;
			},
			
			// 提交表单
			async submitForm() {
				if (!this.validateForm()) return;
				
				try {
					uni.showLoading({ title: '保存中...' });
					
					const user = await this.$leanCloud.user.getCurrent();
					if (!user) {
						uni.redirectTo({
							url: '/pages/login/wechatEntry'
						});
						return;
					}
					
					// 创建宠物对象
					const Pet = this.$leanCloud.Object.extend('Pet');
					const pet = new Pet();
					
					// 设置宠物基本信息
					pet.set('user', user);
					pet.set('avatar', this.formData.avatar);
					pet.set('name', this.formData.name);
					pet.set('type', this.formData.type);
					pet.set('mood', this.formData.mood);
					pet.set('hunger', this.formData.hunger);
					pet.set('energy', this.formData.energy);
					pet.set('exp', this.formData.exp);
					pet.set('level', this.formData.level);
					pet.set('lastFeed', new Date());
					pet.set('lastPlay', new Date());
					pet.set('statistics', this.formData.statistics);
					
					await pet.save();
					
					uni.hideLoading();
					uni.showToast({
						title: '添加成功',
						icon: 'success'
					});
					
					// 返回上一页并刷新
					setTimeout(() => {
						const pages = getCurrentPages();
						const prevPage = pages[pages.length - 2];
						prevPage.$vm.loadPetInfo();
						uni.navigateBack();
					}, 1500);
					
				} catch (error) {
					console.error('保存宠物信息失败:', error);
					uni.hideLoading();
					uni.showToast({
						title: '保存失败',
						icon: 'none'
					});
				}
			},
			
			// 表单验证
			validateForm() {
				if (!this.formData.name.trim()) {
					uni.showToast({
						title: '请输入萌宠名字',
						icon: 'none'
					});
					return false;
				}
				
				return true;
			}
		}
	}
</script>

<style lang="scss">
	.add-pet-container {
		min-height: 100vh;
		background-color: #f8f8f8;
		padding: 30rpx;
		
		.header {
			display: flex;
			align-items: center;
			margin-bottom: 40rpx;
			
			.header-paw {
				width: 48rpx;
				height: 48rpx;
				margin-right: 16rpx;
			}
			
			.title {
				font-size: 36rpx;
				font-weight: bold;
				color: #333;
			}
		}
		
		.form-container {
			background-color: #fff;
			border-radius: 24rpx;
			padding: 30rpx;
			
			.avatar-section {
				display: flex;
				flex-direction: column;
				align-items: center;
				margin-bottom: 40rpx;
				
				.avatar-wrapper {
					position: relative;
					margin-bottom: 16rpx;
					
					.upload-icon {
						position: absolute;
						right: -10rpx;
						bottom: -10rpx;
						width: 60rpx;
						height: 60rpx;
						background-color: #FF9F7A;
						border-radius: 50%;
						display: flex;
						align-items: center;
						justify-content: center;
					}
				}
				
				.upload-text {
					font-size: 26rpx;
					color: #999;
				}
			}
			
			.form-group {
				margin-bottom: 40rpx;
				
				.form-item {
					margin-bottom: 30rpx;
					
					.label {
						display: block;
						font-size: 28rpx;
						color: #333;
						margin-bottom: 16rpx;
					}
					
					.input {
						width: 100%;
						height: 80rpx;
						background-color: #f8f8f8;
						border-radius: 12rpx;
						padding: 0 24rpx;
						font-size: 28rpx;
						color: #333;
					}
					
					.type-selector {
						display: flex;
						justify-content: space-between;
						
						.type-option {
							flex: 1;
							height: 120rpx;
							background-color: #f8f8f8;
							border-radius: 12rpx;
							margin: 0 10rpx;
							display: flex;
							flex-direction: column;
							align-items: center;
							justify-content: center;
							transition: all 0.3s ease;
							
							&.selected {
								background-color: #FFF0EB;
								border: 2rpx solid #FF9F7A;
								
								.type-text {
									color: #FF9F7A;
								}
							}
							
							.type-icon {
								width: 48rpx;
								height: 48rpx;
								margin-bottom: 8rpx;
							}
							
							.type-text {
								font-size: 26rpx;
								color: #666;
							}
						}
					}
				}
			}
			
			.submit-section {
				.submit-btn {
					width: 100%;
					height: 88rpx;
					background: linear-gradient(to right, #FF9F7A, #FFC0A9);
					border-radius: 44rpx;
					color: #fff;
					font-size: 32rpx;
					font-weight: bold;
					display: flex;
					align-items: center;
					justify-content: center;
					
					&:active {
						opacity: 0.8;
					}
				}
			}
		}
	}
</style> 